package cn.gztang.util;
import org.jsoup.Jsoup;
import org.jsoup.safety.Whitelist;

/**
 * jsoup工具类，用于对html文本进行操作
 * @author GaoFeng2017
 * @date 2018-07-09 17:31:30
 **/

public class JsoupUtil {

    /**
     * 过滤白名单
     */
    private static Whitelist whitelist = Whitelist.relaxed();
    /**
     * 白名单初始化
     * */
    static {
        whitelist.addAttributes("span", "style")
                .addAttributes("pre", "style")
                .addAttributes("li","style")
                .addAttributes("p","style")
                .addAttributes("a","target","style");

        char[] chs = {'h','1'};

        for (int i = 0;i < 5;i++) {
            whitelist.addAttributes(String.valueOf(chs),"style");
            chs[1]++;
        }

        chs = null;
    }
    /**
     * 过滤不安全的html文本
     * @author GaoFeng
     * @date 2018/7/11 15:11
     * @param content 要过滤的html文本
     * @return java.lang.String 安全的html文本
     *
     */
    public static String getSafeHtml(String content) {

        if (content == null) {
            return null;
        }

        return Jsoup.clean(content,whitelist);
    }

}
